/*
 * 项   目：spring-boot-demo
 * 作   者：LuoYiMin
 * 邮   箱：1725990142@qq.com
 * 创建时间：2018/9/21 15:37
 */
package cn.roin.learn.demo.service.impl;

import cn.roin.learn.demo.dto.Pageable;
import cn.roin.learn.demo.entity.User;
import cn.roin.learn.demo.entity.UserExample;
import cn.roin.learn.demo.exception.BusinessException;
import cn.roin.learn.demo.mapper.UserMapper;
import cn.roin.learn.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import cn.roin.learn.demo.dto.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * User业务实体类
 */
@Service
public class UserServiceImpl implements UserService {

    /**
     * UserDAO注入
     */
    @Autowired
    UserMapper userMapper;

    /**
     * 分页查询用户信息
     * @param pageable 查询参数封装类实体
     * @return
     */
    @Cacheable(value = "users",keyGenerator = "keyGenerator")
    public PageInfo<User> getPage(Pageable pageable){
        UserExample example = new UserExample();
        UserExample.Criteria criteria = null;
        if (pageable==null)
            pageable = new Pageable();
        else
            criteria = example.createCriteria();
        PageHelper.startPage(pageable);
        if (!StringUtils.isEmpty(pageable.getVal())){
            criteria.andUsernameLike("%"+pageable.getVal()+"%");
        }
        if (!StringUtils.isEmpty(pageable.getVal1()))
            criteria.andEmailEqualTo(pageable.getVal1());

        List<User> list = userMapper.selectByExample(example);
        return new PageInfo<User>(list);
    }

    @Override
    public String save(User user) {
        if (user==null)
            throw  new BusinessException("user为null");
        user.setId(UUID.randomUUID().toString());
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public boolean update(User user) {
        int rows = userMapper.updateByPrimaryKey(user);
        if (rows>1)
            throw new BusinessException("更新失败，存在多个主键为"+user.getId()+"的记录！");
        return rows==1;
    }

    @Override
    public boolean delete(String id) {
        int rows = userMapper.deleteByPrimaryKey(id);
        if (rows>1)
            throw new BusinessException("删除失败，存在多个主键为"+id+"的记录！");
        return rows==1;
    }

    /**
     * 根据主键批量删除
     * 如果当前操作表关联其他业务表，不建议使用此方法，而应该使用deleteBatch_方法
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        UserExample example = new UserExample();
        example.createCriteria().andIdIn(Arrays.asList(ids));
        int rows = userMapper.deleteByExample(example);
        return rows;
    }

    /**
     * 根据主键批量删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch_(String[] ids) {
        int rows = 0;
        for (String id : ids) {
            int row = userMapper.deleteByPrimaryKey(id);
            rows += row;
            if (row==1) {
                //TODO 这里写删除后的其他操作
            }
        }
        return rows;
    }
}
